<?php

namespace app\admin\controller;

use app\common\model\Member;
use app\common\model\MemberFinance;
use app\common\model\MemberPackages;
use app\common\model\Packages;
use app\common\model\TradersOrder;
use app\common\model\UcenterMember;
use think\Db;
use app\common\model\TradersOrder as tradersOrderModel;
use app\common\api\controller\Bonus;
use app\common\api\controller\User as UserApi;
use app\common\api\controller\Error;
use think\Exception;
use think\Validate;


class TradersOrders extends Home
{
    private $paytype_arr = [
        1 => '银行卡',
        2 => '支付宝',
        3 => '微信',
    ];

    private $status_arr = [
        -2 => '<span class="layui-bg-black">已取消</span>',
        0 => '<span class="layui-bg-orange">待付款</span>',
        10 => '已付款',
        15 => '申诉中',
        20 => '已完成',
    ];

    /**
     * Notes:交易订单列表
     * User: Administrator
     * Date: 2023/9/4
     * Time: 18:04
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function lists()
    {
        $where = $this->getMap('update_time');
        $map = $where['map'];
        $maps = $where['maps'];

        $trader_name = input('trader_name');

        if ($trader_name) {
            $trader_ids = \app\common\model\Traders::where('name', 'like', '%' . $trader_name . '%')->column('id');
            $map['trader_id'] = ['in', $trader_ids];
            $maps['trader_name'] = $trader_name;
        }

        $list = tradersOrderModel::where($map)->order('id desc')->paginate(20, false, ['query' => $maps]);
        $page = $list->render();

        foreach ($list as $v) {
            $v->trader_name = \app\common\model\Traders::where('id', $v->trader_id)->value('name');
            $v->usernumber = Member::where('uid', $v->uid)->value('usernumber');
            $v->paytype_name = $v->paytype == -1 ? '待定' : $this->paytype_arr[$v->paytype];
            $v->status_name = $this->status_arr[$v->status];
        }

        $this->assign('_list', $list);
        $this->assign('page', $page);

        /*追加查询条件*/
        $arr = array(
            array(
                'type' => 'input',
                'name' => 'trader_name',
                'title' => '请输入市商名称等有效信息',
                'value' => $trader_name,
                'inputid' => '',
                'readonly' => '',
            )
        );
        $this->searchCondition($maps, $arr);

        $this->assign('email', self::$adminInfo['email']);
        $this->assign('title', '订单列表');
        return $this->fetch();
    }

    /**
     * 处理申诉订单
     */
    public function handleOrder()
    {
        $data = request()->post();
        $id = $data['id'];
        $rowInfo = TradersOrder::find($id);
        if (!$rowInfo) {
            $this->apiError('该订单不存在！');
        }

        if ($rowInfo->status != 15) {
            $this->apiError('该订单不可处理！');
        }

        $remark = '';
        $type = $data['type'];
        switch ($type) {
            case 'agree':
                // 完成订单
                Db::startTrans();
                try {
                    $rowInfo->finish_time = time();
                    $rowInfo->status = 20;
                    $rowInfo->save();

                    if ($rowInfo->recordtype == 1) {
                        // 会员买入
                        $memberModel = MemberFinance::where('uid', $rowInfo->uid)->find();
                        $memberModel->haswealth += $rowInfo->num;
                        $memberModel->allwealth += $rowInfo->num;
                        $memberModel->save();

                        $hasmoney = $memberModel->haswealth;
                        $uid = $rowInfo->uid;
                    } else {
                        // 会员售出
                        $traderModel = \app\common\model\Traders::find($rowInfo->trader_id);
                        $traderModel->hasmoney += $rowInfo->num;
                        $traderModel->totalmoney += $rowInfo->num;
                        $traderModel->save();

                        $hasmoney = $traderModel->hasmoney;
                        $uid = $traderModel->uid;
                        $remark = 'trader';
                    }

                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    $this->apiError('操作失败！');
                }

                $type = array('recordtype' => 1, 'changetype' => 37, 'moneytype' => 3);
                $money = array('money' => $rowInfo->num, 'hasmoney' => $hasmoney);
                money_change($type, $uid, $uid, $money, '', $remark);
                unset($type, $money);

                \app\common\model\Traders::where('id', $rowInfo->trader_id)->setInc('today_num');
                \app\common\model\Traders::where('id', $rowInfo->trader_id)->setInc('total_num');

                break;
            case 'refuse':
                // 取消订单
                Db::startTrans();
                try {
                    tradersOrderModel::offTimeOrder($id);//取消订单

                    if ($rowInfo->recordtype == 0) {
                        // 会员售出，增加
                        $memberModel = MemberFinance::where('uid', $rowInfo->uid)->find();
                        $memberModel->haswealth += $rowInfo->num;
                        $memberModel->save();

                        $hasmoney = $memberModel->haswealth;
                        $uid = $rowInfo->uid;
                    } else {
                        // 会员买入
                        $traderModel = \app\common\model\Traders::find($rowInfo->trader_id);
                        $traderModel->hasmoney += $rowInfo->num;
                        $traderModel->save();

                        $hasmoney = $traderModel->hasmoney;
                        $uid = $traderModel->uid;
                        $remark = 'trader';
                    }

                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    $this->apiError('操作失败！');
                }

                $type = array('recordtype' => 1, 'changetype' => 37, 'moneytype' => 3);
                $money = array('money' => $rowInfo->num, 'hasmoney' => $hasmoney);
                money_change($type, $uid, $uid, $money, '', $remark);
                unset($type, $money);

                break;
        }

        $this->apiSuccess('操作成功！');
    }
}